1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.lang3.tuple;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNull;
21 import static org.junit.Assert.assertTrue;
22
23 import java.util.Calendar;
24 import java.util.HashSet;
25
26 import org.junit.Test;
27
28
29
30
31
32
33 public class TripleTest {
34
35 @Test
36 public void testTripleOf() throws Exception {
37 final Triple<Integer, String, Boolean> triple = Triple.of(0, "foo", Boolean.TRUE);
38 assertTrue(triple instanceof ImmutableTriple<?, ?, ?>);
39 assertEquals(0, ((ImmutableTriple<Integer, String, Boolean>) triple).left.intValue());
40 assertEquals("foo", ((ImmutableTriple<Integer, String, Boolean>) triple).middle);
41 assertEquals(Boolean.TRUE, ((ImmutableTriple<Integer, String, Boolean>) triple).right);
42 final Triple<Object, String, Long> triple2 = Triple.of(null, "bar", Long.valueOf(200L));
43 assertTrue(triple2 instanceof ImmutableTriple<?, ?, ?>);
44 assertNull(((ImmutableTriple<Object, String, Long>) triple2).left);
45 assertEquals("bar", ((ImmutableTriple<Object, String, Long>) triple2).middle);
46 assertEquals(new Long(200L), ((ImmutableTriple<Object, String, Long>) triple2).right);
47 }
48
49 @Test
50 public void testCompatibilityBetweenTriples() throws Exception {
51 final Triple<Integer, String, Boolean> triple = ImmutableTriple.of(0, "foo", Boolean.TRUE);
52 final Triple<Integer, String, Boolean> triple2 = MutableTriple.of(0, "foo", Boolean.TRUE);
53 assertEquals(triple, triple2);
54 assertEquals(triple.hashCode(), triple2.hashCode());
55 final HashSet<Triple<Integer, String, Boolean>> set = new HashSet<Triple<Integer, String, Boolean>>();
56 set.add(triple);
57 assertTrue(set.contains(triple2));
58 }
59
60 @Test
61 public void testComparable1() throws Exception {
62 final Triple<String, String, String> triple1 = Triple.of("A", "D", "A");
63 final Triple<String, String, String> triple2 = Triple.of("B", "C", "A");
64 assertTrue(triple1.compareTo(triple1) == 0);
65 assertTrue(triple1.compareTo(triple2) < 0);
66 assertTrue(triple2.compareTo(triple2) == 0);
67 assertTrue(triple2.compareTo(triple1) > 0);
68 }
69
70 @Test
71 public void testComparable2() throws Exception {
72 final Triple<String, String, String> triple1 = Triple.of("A", "C", "B");
73 final Triple<String, String, String> triple2 = Triple.of("A", "D", "B");
74 assertTrue(triple1.compareTo(triple1) == 0);
75 assertTrue(triple1.compareTo(triple2) < 0);
76 assertTrue(triple2.compareTo(triple2) == 0);
77 assertTrue(triple2.compareTo(triple1) > 0);
78 }
79
80 @Test
81 public void testComparable3() throws Exception {
82 final Triple<String, String, String> triple1 = Triple.of("A", "A", "D");
83 final Triple<String, String, String> triple2 = Triple.of("A", "B", "C");
84 assertTrue(triple1.compareTo(triple1) == 0);
85 assertTrue(triple1.compareTo(triple2) < 0);
86 assertTrue(triple2.compareTo(triple2) == 0);
87 assertTrue(triple2.compareTo(triple1) > 0);
88 }
89
90 @Test
91 public void testComparable4() throws Exception {
92 final Triple<String, String, String> triple1 = Triple.of("B", "A", "C");
93 final Triple<String, String, String> triple2 = Triple.of("B", "A", "D");
94 assertTrue(triple1.compareTo(triple1) == 0);
95 assertTrue(triple1.compareTo(triple2) < 0);
96 assertTrue(triple2.compareTo(triple2) == 0);
97 assertTrue(triple2.compareTo(triple1) > 0);
98 }
99
100 @Test
101 public void testToString() throws Exception {
102 final Triple<String, String, String> triple = Triple.of("Key", "Something", "Value");
103 assertEquals("(Key,Something,Value)", triple.toString());
104 }
105
106 @Test
107 public void testToStringCustom() throws Exception {
108 final Calendar date = Calendar.getInstance();
109 date.set(2011, Calendar.APRIL, 25);
110 final Triple<String, String, Calendar> triple = Triple.of("DOB", "string", date);
111 assertEquals("Test created on " + "04-25-2011", triple.toString("Test created on %3$tm-%3$td-%3$tY"));
112 }
113
114 @Test
115 public void testFormattable_simple() throws Exception {
116 final Triple<String, String, String> triple = Triple.of("Key", "Something", "Value");
117 assertEquals("(Key,Something,Value)", String.format("%1$s", triple));
118 }
119
120 @Test
121 public void testFormattable_padded() throws Exception {
122 final Triple<String, String, String> triple = Triple.of("Key", "Something", "Value");
123 assertEquals(" (Key,Something,Value)", String.format("%1$30s", triple));
124 }
125
126 }
127